﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Website.Models;
using Business.DAO;
using Business.Models;

namespace Website.Controllers
{ 
    public class QuanTriController : Controller
    {
        NhanVienDAO dao = new NhanVienDAO();
        IEnumerable<NhanVien> model = null;

        public ActionResult Index(int NhanVienID = 0, String Function = "")
        {
            ViewBag.Function = Function;

            if (Function != "" && Function == "Delete" && NhanVienID != 0)
            {
                ViewBag.DeleteResult = dao.DeleteQuanTri(NhanVienID); 
                //Tra form lai dang Create Form
                ViewBag.Function = "Create";
            }

            model = dao.GetByType(0); 
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(NhanVien daiLy, String Function = "")
        {
            ViewBag.Function = Function;

            if (Function == "Edit")
            {
                Edit(daiLy);
            }
            else if (Function == "Create")
            {
                Create(daiLy);
            }
            else if (Function == "UpdatePassword")
            {
                UpdatePassword(daiLy);
            }

            model = dao.GetAll();
            return View(model);
        }

        public ActionResult Create(int IdDaiLy = 0)
        {
            return View();
        }

        private void Create(NhanVien daiLy)
        {
            try
            {
                daiLy.NguoiTao = General.GetCurrentUserId();
                daiLy.NgayTao = DateTime.Now;
                ViewBag.CreateResult = dao.CreateQuanTri(daiLy, false);
            }
            catch (Exception ex)
            {
            }
        }

        public ActionResult GridViewPartial()
        {
            try
            {
                model = dao.GetByType(0);
            }
            catch (Exception ex)
            {
            }
            return PartialView("GridView", model);
        }

        public ActionResult Edit(int IdDaiLy)
        {
            NhanVien daiLy = null;
            try
            {

                if (IdDaiLy != 0)
                {
                    model = dao.GetByType(0);
                    daiLy = model.Where(daily => daily.IdNhanVien == IdDaiLy).First();
                }
                else
                {
                    daiLy = new NhanVien();
                }
            }
            catch (Exception ex)
            {
            }
            return View(daiLy);
        }

        private void Edit(NhanVien daiLy)
        {
            try
            {
                if (daiLy.IdNhanVien != 0)
                {
                    ViewBag.EditResult = dao.Edit(daiLy);
                }
            }
            catch (Exception ex)
            {
            }
        }

        public ActionResult UpdatePassword(int IdDaiLy)
        {
            NhanVien daiLy = null;
            try
            {

                if (IdDaiLy != 0)
                {
                    model = dao.GetByType(0);
                    daiLy = model.Where(daily => daily.IdNhanVien == IdDaiLy).First();
                }
                else
                {
                    daiLy = new NhanVien();
                }
            }
            catch (Exception ex)
            {
            }
            return View(daiLy);
        }


        private void UpdatePassword(NhanVien daiLy)
        {
            try
            {
                if (daiLy.IdNhanVien != 0)
                {
                    ViewBag.UpdatePasswordResult = dao.UpdatePassword(daiLy);
                }
            }
            catch (Exception ex)
            {
            }
        }        
    }
}